home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 4 / The Arsenal Files 4 (Arsenal Computer).ISO / casm / au116-as.exe / UTIL / FLIST.CPP < prev    next >
C/C++ Source or Header  |  1994-12-05  |  11KB  |  456 lines

  1. #include "..\au.hpp"
  2.  
  3. #define MAX_HOLD 50
  4.  
  5. static char hold_before[MAX_HOLD][FILE_SIZE], hold_after[MAX_HOLD][FILE_SIZE];
  6. static char hold_found[MAX_HOLD];
  7. static int count=0;
  8.  
  9. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  10. static int is_date(char *string)
  11. {
  12.     if (!(string[0] == ' ' || isdigit(string[0])) || !isdigit(string[1]) ||
  13.         !isdigit(string[3]) || !isdigit(string[4]) ||
  14.         !isdigit(string[6]) || !isdigit(string[7]) ||
  15.         string[2]!='-' || string[5]!='-')
  16.     {
  17.         return FALSE;
  18.     }
  19.     else
  20.         return TRUE;
  21. }
  22. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  23. void add_to_flist(AU *au, char *string, int append)
  24. {
  25.     for (int i=0 ; i < au->flist_pos ; i++)
  26.     {
  27.         if (stricmp(au->flist[i], string)==0) // see if not already in the list
  28.             return;
  29.     }
  30.     if (au->flist_pos == MAX_FLISTS)
  31.     {
  32.         au_printf_error(au, "\nToo Many File Lists Specified");
  33.         exit(1);
  34.     }
  35.     au->append_flist[au->flist_pos] = append;
  36.     strcat(au->flist[au->flist_pos++], string);
  37.     return;
  38. }
  39. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  40. static int desc_file_find(AU *au, char *arc_file_name, char *actual_name)
  41. {
  42.     ARC_HANDLE arc_handle;
  43.     ARC_RECORD record;
  44.     int found = FALSE;
  45.     int retCode;
  46.  
  47.     arc_handle.init(au, arc_file_name);
  48.     if (arc_handle.type > 0)
  49.     {
  50.         for (;;)
  51.         {
  52.             retCode = arc_handle.get_record(au, &record);
  53.             if (retCode == EOF)
  54.                 break;
  55.             else if (retCode == -2)
  56.             {
  57.                 add_to_bad_list(au, au->source_directory, arc_file_name, 0);
  58.                 break;
  59.             }
  60.             if (wildcard_compare(au, record.name, au->desc_file))
  61.             {
  62.                 strcpy(actual_name, record.name);
  63.                 found = TRUE;
  64.                 break;
  65.             }
  66.         }
  67.     }
  68.     arc_handle.deinit(au);
  69.     return found;
  70. }
  71. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  72. static int get_file_description(AU *au, char *arc_file_name, char *description)
  73. {
  74.     char partial_save[FLENGTH];
  75.     char actual_name[FLENGTH];
  76.     HANDLE handle;
  77.     int retCode = FALSE;
  78.  
  79.     if (desc_file_find(au, arc_file_name, actual_name))
  80.     {
  81.         mkdir(TEMP_DIR);
  82.  
  83.         strcpy(partial_save, au->partial);
  84.         strcpy(au->partial, actual_name);
  85.         unarc(au, arc_file_name, TEMP_DIR, NULL, 0, FALSE);
  86.         strcpy(au->partial, partial_save);
  87.  
  88.         if (access(actual_name, 0x00) == 0)
  89.         {
  90.             cd(au, TEMP_DIR);
  91.             handle.open(au, actual_name, O_RDONLY|O_TEXT);
  92.             memset(description, '\0', DESC_SIZE);
  93.             handle.read(description, DESC_SIZE-1);
  94.             string_replace(description, "\n", " ");
  95.             string_replace(description, "\r", " ");
  96.             handle.close();
  97.             unlink(actual_name);
  98.             cd(au, "..");
  99.             retCode = TRUE;
  100.         }
  101.  
  102.         rmdir(TEMP_DIR);
  103.     }
  104.  
  105.     return retCode;
  106. }
  107. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  108. static int write_description(AU *au, HANDLE *handle, char *arc_file_name)
  109. {
  110.     char description[DESC_SIZE];
  111.  
  112.     if (au->desc_file[0] != '\0')
  113.     {
  114.         if (get_file_description(au, arc_file_name, description))
  115.         {
  116.             handle->write_text(description);
  117.             handle->write_text("\r\n");
  118.             return 0;
  119.         }
  120.     }
  121.  
  122.     handle->write_text(au->description);
  123.     handle->write_text("\r\n");
  124.     return 0;
  125. }
  126. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  127. static int file_exists(char *file_name, char *flf_name)
  128. {
  129.     char temp[80], path[80], file[80];
  130.     struct ffblk ffblk;
  131.  
  132.     if (findfirst(file_name, &ffblk, 0) != 0)
  133.     {
  134.         /* Peel the path out of the file list file name and check there too */
  135.         strcpy(temp, flf_name);
  136.         split_file(temp, path, file);
  137.         if (path[0] == '\0')
  138.             return FALSE;
  139.         strcat(path, file_name);
  140.         return (findfirst(path, &ffblk, 0) == 0);
  141.     }
  142.     return TRUE;
  143. }
  144. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  145. static void search_file(AU *au, HANDLE *handle, char *file_name, int fpos)
  146. {
  147.     int  style;
  148.     long pos;
  149.     int  len;
  150.     long size;
  151.     char size_string[12];
  152.     char date_string[9];
  153.     char string[250];       /* Should be big enough for most descriptions */
  154.     char string2[FLENGTH];
  155.     char string3[200];
  156.     DATE date;
  157.     int  i;
  158.     struct ffblk ffblk;
  159.  
  160.     /* Just in case files.bbs is completely empty, set the style */
  161.     if (au->force_mode > 0)
  162.         style = au->force_mode;
  163.     else
  164.         style = OPUS;
  165.  
  166.     pos = 0;
  167.     for(EVER)            /* While not EOF */
  168.     {
  169.         pos = handle->seek(0, SEEK_CUR);
  170.         if (handle->read_raw_line(string) == EOF)
  171.             break;
  172.         len = strlen(string);
  173.         ltrim(string);
  174.         pos+=len-strlen(string);
  175.         if (string[0]=='-')            // Ignore comments
  176.             continue;
  177.  
  178.  /* Determine style of file */
  179.         if (au->force_mode > 0)
  180.             style = au->force_mode;
  181.         else if (is_date(string+17))
  182.             style = GT;
  183.         else if (is_date(string+23))
  184.             style = RBBS;
  185.         else
  186.             style = OPUS;
  187.  
  188.         i=0;                          /* peel filename out */
  189.         while (string[i] > ' ')
  190.         {
  191.             string2[i] = string[i];
  192.             i++;
  193.         }
  194.         string2[i] = '\0';
  195.  
  196.         for (int k=0; k<count; k++)
  197.         {
  198.             char *before = hold_before[k];
  199.             char *after  = hold_after[k];
  200.             int  match     = FALSE;
  201.  
  202.             if (stricmp(string2, before) == 0)
  203.                 match = TRUE;
  204.             else               /* See if the extension is different */
  205.             {
  206.                 char *ptr1, *ptr2;
  207.  
  208.                 /* but make sure the file doesn't exist first */
  209.                 if (!file_exists(string2, file_name))
  210.                 {
  211.                     ptr1 = strchr(string2, '.');
  212.                     ptr2 = strchr(before, '.');
  213.                     if (ptr1 != NULL && ptr2 != NULL &&
  214.                         ptr1-string2 == ptr2-before  &&
  215.                         strnicmp(string2, before, ptr1-string2)==0)
  216.                     {
  217.                         match = TRUE;
  218.                         if (strchr(file_name, ':') || strchr(file_name, '\\'))
  219.                             sprintf(string3, "Replaced: %-15s with %-15s in %s",
  220.                                      string2, after, file_name);
  221.                         else
  222.                         {
  223.                             sprintf(string3, "Replaced: %-15s with %-15s in %s",
  224.                                      string2, after, au->source_directory);
  225.                             build_fname(string3, string3, file_name);
  226.                         }
  227.                         au->log.write(au, au->flist_log, string3);
  228.                         au->log.write(au, au->flist_log, "\n");
  229.                     }
  230.                 }
  231.             }
  232.  
  233.             if (match)         /* fix string, then write it back out */
  234.             {
  235.                 int i=0,j;
  236.  
  237.                 /* Don't do anything if the file is missing */
  238.                 if (findfirst(after, &ffblk, 0))
  239.                     return;
  240.  
  241.                 size = ffblk.ff_fsize;
  242.                 fdate_to_date(&date, &ffblk);
  243.                 strcpy(date_string, date_to_string(&date));
  244.  
  245. /* put the new filename in the string */
  246.                 while (after[i] > ' ')
  247.                 {
  248.                     string[i] = toupper(after[i]);
  249.                     i++;
  250.                 }
  251.                 while (i < strlen(before))
  252.                     string[i++] = ' ';
  253.  
  254.                 if (style == GT || style == RBBS)
  255.                 {
  256.                     if (style == GT)
  257.                         strcpy(size_string, left_pad(my_ltoa(size/1024) ,3));
  258.                     else
  259.                         strcpy(size_string, left_pad(my_ltoa(size), 8));
  260.  
  261.                     j=13; i=0;
  262.                     while (size_string[i]!='\0')
  263.                         string[j++] = size_string[i++];
  264.  
  265.                     if (style == GT)
  266.                     {
  267.                         j = 17;
  268.                         if (date_string[0] == '0')
  269.                             date_string[0] = ' ';
  270.                     }
  271.                     else
  272.                     {
  273.                         j = 23;
  274.                         if (date_string[0] == ' ')
  275.                             date_string[0] = '0';
  276.                     }
  277.                     i=0;
  278.                     while (date_string[i]!='\0')
  279.                         string[j++] = date_string[i++];
  280.                 }
  281.                 handle->seek(pos, SEEK_SET);
  282.                 handle->write_text(string);
  283.                 hold_found[k] = TRUE;
  284.                 break;
  285.             }
  286.         }
  287.     }
  288.     if (au->append_flist[fpos]==TRUE)
  289.     {
  290.         for (i=0; i<count; i++)
  291.         {
  292.             if (!hold_found[i])
  293.             {
  294.                 findfirst(hold_after[i], &ffblk, 0);
  295.                 size = ffblk.ff_fsize;
  296.                 date.year = (((ffblk.ff_fdate & 0xFE00) >> 9) + 1980) % 100;
  297.                 date.month = (ffblk.ff_fdate & 0x01E0) >> 5;
  298.                 date.day = ffblk.ff_fdate & 0x001F;
  299.                 strcpy(date_string, date_to_string(&date));
  300.  
  301.                 handle->seek(0, SEEK_END);
  302.                 handle->write_text(right_pad(hold_after[i],13));
  303.                 if (style == GT)
  304.                 {
  305.                     handle->write_text(left_pad(my_ltoa(size/1024), 3));
  306.                     handle->write_text(" ");
  307.                     if (date_string[0] == '0')
  308.                         date_string[0] = ' ';
  309.                     handle->write_text(date_string);
  310.                     handle->write_text(" ");
  311.                 }
  312.                 else if (style == RBBS)
  313.                 {
  314.                     handle->write_text(left_pad(my_ltoa(size), 8));
  315.                     handle->write_text("  ");
  316.                     if (date_string[0] == ' ')
  317.                         date_string[0] = '0';
  318.                     handle->write_text(date_string);
  319.                     handle->write_text("  ");
  320.                 }
  321.  
  322.                 write_description(au, handle, hold_after[i]);
  323.  
  324.                 if (strchr(file_name, ':') || strchr(file_name, '\\'))
  325.                     sprintf(string3, "Added: %-15s to %s", hold_after[i], file_name);
  326.                 else
  327.                 {
  328.                     sprintf(string3, "Added: %-15s to %s", hold_after[i],
  329.                             au->source_directory);
  330.                     build_fname(string3, string3, file_name);
  331.                 }
  332.                 au->log.write(au, au->flist_log, string3);
  333.                 au->log.write(au, au->flist_log, "\n");
  334.             }
  335.         }
  336.     }
  337.     return;
  338. }
  339. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  340. ////////////////////////////////////////////////////////////////////
  341. //                0          1         2          3
  342. //                0123456789012345678901234567890123456
  343. //                -------------------------------------
  344. //    Opus        FILENAME.EXT  Description
  345. //    RBBS        FILENAME.EXT 99999999  12-31-99  Description
  346. //    GT-Power    FILENAME.EXT 999 12-31-99 Description  (front 0 stripped on date)
  347. ////////////////////////////////////////////////////////////////////
  348. void fix_flist(AU *au, char *before, char *after)
  349. {
  350.     HANDLE handle;
  351.     long pos,pos2;
  352.     int  c,d,i;
  353.  
  354.     if (before != NULL)
  355.     {
  356.         strcpy(hold_before[count], before);
  357.         strcpy(hold_after[count], after);
  358.         hold_found[count] = FALSE;
  359.         count++;
  360.  
  361.         if (count < MAX_HOLD)
  362.             return;
  363.     }
  364.     else if (count == 0)
  365.         return;
  366.  
  367.     if (au->flist_pos > 0)
  368.         au_printf_c(au, 15, "\n   Updating file list files...\n");
  369.  
  370. /**********************************************************************/
  371.     for (int fpos = 0 ; fpos < au->flist_pos; fpos++)
  372.     {
  373.         char *file_name = au->flist[fpos];
  374.  
  375.         if (file_name[0] == '\0')
  376.             continue;
  377.  
  378.         if (handle.open(au, file_name, O_RDWR|O_BINARY) != SUCCESS)
  379.         {
  380.             au_printf_error(au, "\nFile Listing File '%s' not found", file_name);
  381.             au->flist[fpos][0] = '\0';     // One warning is enough
  382.             continue;
  383.         }
  384.         search_file(au, &handle, file_name, fpos);
  385.         handle.close();
  386.     }
  387.     count = 0;
  388.     return;
  389. }
  390. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  391. /* Get the next file name from a files.bbs file */
  392. int get_next_file_name(AU *, HANDLE *handle, char *ret_string, int *area)
  393. {
  394.     char string[256];
  395.     char string2[256];
  396.     int  good, i;
  397.  
  398.     for (;;)
  399.     {
  400.         if (handle->read_raw_line(string)==EOF)
  401.             return EOF;
  402.  
  403.         /* ignore headers and such */
  404.         if (string[0] == '-' || string[0] == ' ')
  405.             continue;
  406.  
  407.         /* Check for a space within the first 13 characters */
  408.         good = TRUE;
  409.         for (i=0; i < FILE_SIZE; i++)
  410.         {
  411.             if (string[i] == '\0')
  412.                 break;
  413.  
  414.             if (string[i] == ' ' || string[i] == '\t')
  415.             {
  416.                 while (i < FILE_SIZE)
  417.                 {
  418.                     if (string[i] == '\0')
  419.                         break;
  420.  
  421.                     if (string[i] != ' ' && string[i] != '\t')
  422.                     {
  423.                         good = FALSE;
  424.                         break;
  425.                     }
  426.                     i++;
  427.                 }
  428.                 break;
  429.             }
  430.             if (string[i] < ' ' || string[i] == '?' || string[i] == '*') // string[i] > 127)
  431.             {
  432.                 good = FALSE;
  433.                 break;
  434.             }
  435.         }
  436.         if (!good)
  437.             continue;
  438.  
  439.         split_string(string, string2);
  440.         if (stricmp(string2, "area") == 0 && area != NULL)
  441.         {
  442.             *area = atol(string);
  443.         }
  444.  
  445.         if (strlen(string2) > FILE_SIZE ||
  446.             string2[0] == '.' || !is_dos_name(string2) ||
  447.             (string2[0] >= 'a' && string2[0] <= 'z'))
  448.         {
  449.             continue;
  450.         }
  451.         strcpy(ret_string, string2);
  452.         return 0;
  453.     }
  454. }
  455.  
  456.